
import socket
import threading
import time
import sys
import tqdm   #增加进度条显示

number = 0
upport = ""
def one_port(ip,port):
	"我可以扫描一个端口的状态"
	client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#	ip = input("请输入要扫描的ip地址：")
#	port = int(input("请输入要扫描的端口："))
	data = client.connect_ex((ip,port))
	if data == 0:
		global number
		global upport
#		print(f"端口{port}状态：open")
		upport += f"端口{port}状态：open\n"
		number += 1
	client.close()

def more_port():
	"我可以扫描多个端口的状态"
#	client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
	try:
		ip = input("请输入要扫描的ip地址：")
		startPort = int(input("请输入要扫描的起始端口："))
		endPort = int(input("请输入要扫描的结束端口：")) + 1
		startTime = time.time()
		for i in tqdm.tqdm(range(startPort,endPort)):
			t = threading.Thread(target=one_port,args=(ip,i))
			t.start()
		endTime = time.time()
		print(upport)
		print(f"共有{number}端口开启!  共花费了{endTime-startTime:.3f}秒")
	except KeyboardInterrupt:
		print("\n谢谢使用，再见！")
		sys.exit()

if __name__ == '__main__':
	more_port()